
import wx
from wx import Notebook
import wx.lib.flatnotebook as fnb
from wx.lib.flatnotebook import FlatNotebook

import os.path
from CodeTab import CodeTab

class DefaultNoteBook(Notebook):
	def __init__(self, parent, id):
		Notebook.__init__(self, parent, id, style = wx.CLIP_CHILDREN)


class FlatNoteBook(FlatNotebook):
	def __init__(self, parent, id):
		FlatNotebook.__init__(self, parent, id, style = fnb.FNB_X_ON_TAB|fnb.FNB_FF2)

		self.Bind(wx.lib.flatnotebook.EVT_FLATNOTEBOOK_PAGE_CLOSED, self.OnCloseTab)
		#self.Bind(wx.lib.flatnotebook.EVT_FLATNOTEBOOK_PAGE_CHANGED, self.OnSelectTab)

		self.GetRelativePath = lambda f: f
		self.OnCloseTabHandler = None

		self.NewCounter = 0
	

	def AddTab(self, title, filepath = None):
		tab = CodeTab(self)
		if filepath:
			tab.LoadSourceFromPath(filepath)
		self.AddPage(tab, title)
		tab.SetFocus()
		tab.SetTabInfo(title, self)
		return tab

	def NewTab(self):
		tabtitle = 'Untitled - ' + str(self.NewCounter)
		self.NewCounter += 1

		tab = self.AddTab(tabtitle)

		tab.filepath = None
		tab.relativepath = None

	def Open(self, filepath):
		n = self.GetPageCount()
		tab = None
		for i in range(n):
			thistab = self.GetPage(i)
			tabfile = thistab.filepath
			tabfile = os.path.normpath(os.path.abspath(tabfile))
			needfile = os.path.normpath(os.path.abspath(filepath))
			if tabfile == needfile:
				tab = thistab
				self.SetSelection(i)
				break
		else:
			tab = self.OpenTab(filepath)

		return tab

	def OpenTab(self, filepath):
		filepath, tabtitle, relativepath = self.ParsePath(filepath)

		tab = self.AddTab(tabtitle, filepath)

		tab.filepath = filepath
		tab.relativepath = relativepath

		return tab

	def CloseTab(self, n):
		if self.GetPageCount() <= n:
			return
		self.DeletePage(n)
	
	def CloseCurrentTab(self):
		n = self.GetSelection()
		if n >= 0:
			self.CloseTab(n)

	def GetTabTitle(self, dir):
		if self.GetRelativePath:
			return self.GetRelativePath(dir)
		else:
			return dir

	def ParsePath(self, filepath):
		filepath = os.path.normpath(os.path.abspath(filepath))
		tabtitle = os.path.basename(filepath)
		relativepath = self.GetRelativePath(filepath)
		return filepath, tabtitle, relativepath

	def UpdateTabPath(self, tab, filepath):
		filepath, tabtitle, relativepath = self.ParsePath(filepath)
		tab.filepath = filepath
		tab.relativepath = relativepath
		tab.SetTabTitle(tabtitle)

	def CurrentTab(self):
		return self.GetPage(self.GetSelection())

	##################################################
	def RegisterGetTabTitle(self, handler):
		self.GetRelativePath = handler

	def RegisterCloseTab(self, handler):
		self.OnCloseTabHandler = handler

	##################################################

	def OnCloseTab(self, event):
		if self.OnCloseTabHandler:
			self.OnCloseTabHandler()

	def OnSelectTab(self, event):
		#wx.CallAfter(self.DoSetFocus, event.GetSelection())
		wx.CallAfter(self.DoSetFocus)
		event.Skip(True)
	
	def DoSetFocus(self):
		#self.GetPage(n).SetFocus()
		self.CurrentTab().editor.SetFocus()

	##################################################







NoteBook = DefaultNoteBook
NoteBook = FlatNoteBook


##################################################

def runTest(frame, nb, log):
	p = NoteBook(frame, -1)
	p.OpenTab('wxrun.py')
	p.OpenTab('Misc.py')
	p.OpenTab('json.py')

	return p

if __name__ == '__main__':
    import sys,os
    import wxrun
    wxrun.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])

